var $ = require("../../include/$");
var tablename = "message";
module.exports = {
    post: function(req, res) {
        // console.log(req.body);
        req.models.message.create($.extend("", req.body), function(err, result) {
            if (err) {
                // console.log(err);
                res.send(500, err);
            } else {
                res.send(true);
            }
        });
    },

    get: function(req, res) {

        // console.log("查询表“", tablename, "”的数据", req.body, req.query);
        var finner = req.models[tablename].find({
            "type": 1,
        });
        require("../../include/RESTful_query").orm_get(finner, req.query);

        finner.run(function(err, result) {
            if (err) {
                res.send(500, err);
            } else {
                res.send(result);
            }
        });
    },

    put: function(req, res) {
        var msgid = req.body.msgid;
        // console.log("为表“", tablename, "”进行修改：", req.body, req.query);
        req.models[tablename].get(msgid, function(err, result) {
            if (err) {
                res.send(500, err);
            } else {
                for (req.body in result) {
                    result['type'] = 2;
                };
                result.save(function(err) {
                    if (err) {
                        res.send(500, err);
                    } else {
                        res.send(result);
                    }
                });
            }
        });
    }
}